﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Transaction.recPaymenthandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Administration;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;

using SIPO.Library.Enum;
using SIPO.Web.Support;

using Newtonsoft.Json.Linq;
namespace SIPO.Web.AjaxHandler.Transaction
{
    
    
    public class recPaymenthandler : SIPO.Web.BaseHandler
    {

        private  recPaymentRepo recPaymentDal = new recPaymentRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

        private recLogRepo logDal = new recLogRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private string MethodName = string.Empty;

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];

            context.Response.Clear();
            context.Response.Buffer = true;

            System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);

            string jsonString = string.Empty;
            jsonString = context.Server.UrlDecode(sr.ReadToEnd());
            JObject jObj = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);

            sr.Close();
            sr.Dispose();

            switch (MethodName.ToLower())
            {
                case ("insert"):
                    context.Response.Write(Insert(context, jObj));
                    break;
                case ("update"):
                    context.Response.Write(Update(context, jObj));
                    break;
                case ("delete"):
                    context.Response.Write(Delete(context, jObj));
                    break;
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;
                case ("updatepembayaran"):
                    context.Response.Write(UpdatePembayaran(context, jObj));
                    break;
                default:
                    // do nothing
                    break;
            }

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }


        private string Insert(HttpContext context, JObject jo)
        {
            JSONHelper helper = new JSONHelper();

            try
            {
                recPayment obj = new recPayment();
                obj.PaymentDate = Helper.FormatDate((string)jo["PaymentDate"], Helper.Culture.Indonesian);
                //obj.PaymentNo = "8." + DateTime.Now.ToString("MM") + "." + DateTime.Now.ToString("yy") + "." + recPaymentDal.GetMaxID().ToString().PadLeft(5, '0');
                //obj.PaymentNo = recPaymentDal.ge
                obj.PaymentValue = Helper.ToDecimal(jo["PaymentValue"].ToString(), System.Globalization.NumberStyles.Number, Helper.Culture.Indonesian);
                obj.InvoiceNo = (string)jo["InvoiceNo"];
                obj.DebtorSectorID = (Int16)jo["DebtorSectorID"];
                obj.TransDate = DateTime.Now;
                obj.Remark = (Int16)jo["DebtorSectorID"] == 0 ? (string)jo["Remark"] : string.Empty;
                
                obj.CreatedDate = DateTime.Now;
                obj.CreatedBy = base.UserLoggedIn;

                recPaymentDal.SavePembayaran(obj);

                helper.Message = "Data berhasil di simpan !";
                helper.IsSuccess = true;                
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Insert";
                log.LogMessage = helper.Message;
                log.LogSource = "recPaymenthandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }


        private string UpdatePembayaran(HttpContext context, JObject jo)
        {
            JSONHelper helper = new JSONHelper();

            try
            {
               
                //List<recInvoice> listInvoice = new List<recInvoice>();
                List<recPayment> listPayment = new List<recPayment>();
                var strId = context.Request.Params["InvoiceNo"];

                string[] ids = strId.Split(',');
                foreach (string id in ids)
                {
                    // urai Array Concat dari InvoiceNo | PaymentValue | DebtorSectorID | Nama Perusahaan
                    
                    string[] idInvoices = id.Split('|');
                    //Array Concat InvoiceNo
                    string InvoiceNo = idInvoices[0].ToString();
                    //Array Concat PaymentValue
                    decimal paymentValue = Helper.ToDecimal(idInvoices[1].ToString().Trim(), System.Globalization.NumberStyles.Number, Helper.Culture.English); ;// Convert.ToDecimal(idInvoices[1].ToString());
                    //Array Concat DebtorSectorID
                    int DebtorSectorID = Convert.ToInt32(idInvoices[2].ToString().Trim());
                    //Array Concat Nama  Perusahaan
                    string NamaPerusahaan = idInvoices[3].ToString().Trim();
                    //end
                   
                    // Data Invoice
                    recInvoice objInvoice = recInvoiceDal.GetById(InvoiceNo);
                    //end


                    //Data Payment
                    recPayment obj = new recPayment();
                    obj.PaymentDate = DateTime.ParseExact(jo["PaymentDate"].ToString(), "dd/MM/yyyy", new System.Globalization.CultureInfo("id-ID"));
                    //obj.PaymentNo = "8." + DateTime.Now.ToString("MM") + "." + DateTime.Now.ToString("yy") + "." + recPaymentDal.GetMaxID().ToString().PadLeft(5, '0');
                    obj.PaymentValue = paymentValue;
                    obj.InvoiceNo = InvoiceNo;
                    obj.DebtorSectorID = objInvoice.DebtorSectorID;
                    obj.TransDate = DateTime.Now;
                    
                    if(DebtorSectorID==0)
                        obj.Remark =NamaPerusahaan;
                    
                    obj.CreatedDate = DateTime.Now;
                    obj.CreatedBy = base.UserLoggedIn;

                    listPayment.Add(obj);

                    //end
                    
                }

                    recPaymentDal.UpdatePembayaran(listPayment);
                    helper.Message = "Data berhasil di simpan !";
                    helper.IsSuccess = true;
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Insert";
                log.LogMessage = helper.Message;
                log.LogSource = "recPaymenthandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        private string Update(HttpContext context, JObject jo)
        {
            JSONHelper helper = new JSONHelper();

            try
            {
                
                recPayment obj = new recPayment();
                recPayment objGet  = recPaymentDal.GetById((int)jo["PaymentID"]);
                if (obj != null)
                {
                    obj.PaymentID = (int)jo["PaymentID"];
                    obj.PaymentDate = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", new System.Globalization.CultureInfo("id-ID"));
                    obj.PaymentNo = objGet.PaymentNo;
                    obj.PaymentValue = Helper.ToDecimal(jo["PaymentValue"].ToString(), System.Globalization.NumberStyles.Number, Helper.Culture.Indonesian); 
                    obj.InvoiceNo = (string)jo["InvoiceNo"];
                    obj.DebtorSectorID = (Int16)jo["DebtorSectorID"];
                    obj.TransDate = DateTime.Now;
                    obj.Remark = (Int16)jo["DebtorSectorID"] == 0 ? (string)jo["Remark"] : string.Empty ;
                    obj.UpdatedDate = DateTime.Now;
                    obj.UpdatedBy = base.UserLoggedIn;

                    recPaymentDal.EditPembayaran(obj);


                    helper.Message = "Data berhasil di ubah !";
                    helper.IsSuccess = true;
                }
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Update";
                log.LogMessage = helper.Message;
                log.LogSource = "recPaymenthandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }


        private string Delete(HttpContext context, JObject jo)
        {
           

            JSONHelper helper = new JSONHelper();
            try
            {

                var strId = (string)jo["PaymentID"];

                List<recPayment> listPayment = new List<recPayment>();
                recPayment obj = new recPayment();

                string[] ids = strId.Split(',');
                foreach (string id in ids)
                {
                    //int intID = Convert.ToInt32(id);
                    //recPaymentDal.Delete(intID);

                    obj = recPaymentDal.GetById(Convert.ToInt32(id));
                    listPayment.Add(obj);
                    
                }

                recPaymentDal.DeletePembayaran(listPayment);
                
                helper.IsSuccess = true;
                helper.Message = "Data berhasil di hapus !";
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Delete";
                log.LogMessage = helper.Message;
                log.LogSource = "recPaymenthandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);

            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        private string GetData(HttpContext context)
        {
            string queryRegistrasi = string.Empty;
            string queryPayment = string.Empty;
            string queryAksi = string.Empty;
            string parameters = Helper.JqGridFilterToSQLParams(context);

            #region Query Payment
            queryPayment = @"
                        SELECT
                        recPayment.PaymentID,
                        recPayment.TransDate,
                        recPayment.PaymentNo,
                        recPayment.PaymentDate,
                        recPayment.PaymentValue,
                        recPayment.InvoiceNo,
                        recPayment.DebtorSectorID,
                        recPayment.PostingDate,
                        recPayment.PostingBy,
                        recPayment.CreatedDate,
                        recPayment.CreatedBy,
                        recPayment.UpdatedDate,
                        recPayment.UpdatedBy,
                        CASE WHEN recPayment.DebtorSectorID = 0 THEN recPayment.Remark ELSE recDebtor.DebtorName END DebtorName ,
                        CASE WHEN recPayment.DebtorSectorID = 0 THEN
                        (SELECT recSubSubSector.SubSubSectorName
                        FROM transactions.recSectorRegistration
                        LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                        WHERE recSectorRegistration.InvoiceNo = recPayment.InvoiceNo
                        GROUP BY recSubSubSector.SubSubSectorName
                        )ELSE recSubSubSector.SubSubSectorName END AS SubSubSectorName,
                        recInvoice.InvoiceValue,
                        CASE WHEN recPayment.DebtorSectorID = 0 THEN
                        (SELECT recSector.SectorName
                        FROM transactions.recSectorRegistration
                        LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                        LEFT JOIN master.recSubSector on recSubSubSector.SubSectorID = recSubSector.SubSectorID
                        LEFT JOIN master.recSector on recSubSector.SectorID = recSector.SectorID
                        WHERE recSectorRegistration.InvoiceNo = recPayment.InvoiceNo
                        GROUP BY recSector.SectorName
                        )ELSE recSector.SectorName END AS SectorName,
                        CASE WHEN recPayment.DebtorSectorID = 0 THEN
                        (SELECT recSubSector.SubSectorName
                        FROM transactions.recSectorRegistration
                        LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                        LEFT JOIN master.recSubSector on recSubSubSector.SubSectorID = recSubSector.SubSectorID
                        WHERE recSectorRegistration.InvoiceNo = recPayment.InvoiceNo
                        GROUP BY recSubSector.SubSectorName
                        )ELSE recSubSector.SubSectorName END AS SubSectorName,
                        recUserSector.UserID
                        FROM
                        transactions.recPayment  LEFT join transactions.recSectorRegistration  on recPayment.InvoiceNo = recSectorRegistration.InvoiceNo
                        LEFT join master.recUserSector on recSectorRegistration.SectorRegistrationID = recUserSector.RegisterSectorID
                        LEFT JOIN master.recDebtorSector ON recPayment.DebtorSectorID=recDebtorSector.DebtorSectorID
                        LEFT JOIN master.recSubSubSector ON recSubSubSector.SubSubSectorID =recDebtorSector.SubSubSectorID
                        LEFT JOIN master.recSector ON recDebtorSector.SectorID=recSector.SectorID
                        LEFT JOIN master.recSubSector ON recDebtorSector.SubSectorID=recSubSector.SubSectorID
                        LEFT JOIN master.recDebtor ON recDebtorSector.DebtorID = recDebtor.DebtorID
                        LEFT JOIN transactions.recInvoice ON recPayment.InvoiceNo = recInvoice.InvoiceNo
                        ";
            #endregion

            #region Query Registrasi
            queryRegistrasi = @"SELECT
                                recPayment.PaymentID,

                                recPayment.TransDate,
                                recPayment.PaymentNo,
                                recPayment.PaymentDate,
                                recPayment.PaymentValue,
                                recPayment.InvoiceNo,
                                recPayment.DebtorSectorID,
                                recPayment.PostingDate,
                                recPayment.PostingBy,
                                recPayment.CreatedDate,
                                recPayment.CreatedBy,
                                recPayment.UpdatedDate,
                                recPayment.UpdatedBy,
                                CASE WHEN recPayment.DebtorSectorID = 0 THEN recPayment.Remark ELSE '' END DebtorName ,
                                CASE WHEN recPayment.DebtorSectorID = 0 THEN
                                (SELECT recSubSubSector.SubSubSectorName
                                FROM transactions.recSectorRegistration
                                LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                                WHERE recSectorRegistration.InvoiceNo = recPayment.InvoiceNo
                                GROUP BY recSubSubSector.SubSubSectorName
                                )ELSE recSubSubSector.SubSubSectorName END AS SubSubSectorName,
                                recInvoice.InvoiceValue,
                                CASE WHEN recPayment.DebtorSectorID = 0 THEN
                                (SELECT recSector.SectorName
                                FROM transactions.recSectorRegistration
                                LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                                LEFT JOIN master.recSubSector on recSubSubSector.SubSectorID = recSubSector.SubSectorID
                                LEFT JOIN master.recSector on recSubSector.SectorID = recSector.SectorID
                                WHERE recSectorRegistration.InvoiceNo = recPayment.InvoiceNo
                                GROUP BY recSector.SectorName
                                )ELSE recSector.SectorName END AS SectorName,
                                CASE WHEN recPayment.DebtorSectorID = 0 THEN
                                (SELECT recSubSector.SubSectorName
                                FROM transactions.recSectorRegistration
                                LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                                LEFT JOIN master.recSubSector on recSubSubSector.SubSectorID = recSubSector.SubSectorID
                                WHERE recSectorRegistration.InvoiceNo = recPayment.InvoiceNo
                                GROUP BY recSubSector.SubSectorName
                                )ELSE recSubSector.SubSectorName END AS SubSectorName,
                                recUserSector.UserID
                                FROM
                                transactions.recPayment  inner join transactions.recSectorRegistration  on recPayment.InvoiceNo = recSectorRegistration.InvoiceNo
                                inner join master.recUserSector on recSectorRegistration.SectorRegistrationID = recUserSector.RegisterSectorID
                                LEFT JOIN master.recSector ON recSectorRegistration.SectorID = recSector.SectorID
                                LEFT JOIN master.recSubSector ON recSectorRegistration.SubSectorID = recSubSector.SubSectorID
                                LEFT JOIN master.recSubSubSector ON recSectorRegistration.SubSubSectorID = recSubSubSector.SubSubSectorID
                                LEFT JOIN transactions.recInvoice ON recPayment.InvoiceNo = recInvoice.InvoiceNo
                                where recPayment.DebtorSectorID = 0";
            #endregion
            
            if (base.DebtorID != null)
            {
                queryRegistrasi = queryRegistrasi + "  AND recUserSector.UserID =" + base.UserID;
                queryPayment = queryPayment + " WHERE recdebtor.DebtorID = " + base.DebtorID;
            }
            else
            {
                if (base.UserTypeID == 1)
                {
                    queryRegistrasi = queryRegistrasi + "  AND recUserSector.UserID =" + base.UserID;
                    queryPayment = queryPayment + " WHERE recdebtor.DebtorID IS NULL ";
                }
                else
                {
                    #region Query Aksi
                    queryAksi = @"
                                SELECT
                                recPayment.PaymentID,
                                recPayment.TransDate,
                                recPayment.PaymentNo,
                                recPayment.PaymentDate,
                                recPayment.PaymentValue,
                                recPayment.InvoiceNo,
                                recPayment.DebtorSectorID,
                                recPayment.PostingDate,
                                recPayment.PostingBy,
                                recPayment.CreatedDate,
                                recPayment.CreatedBy,
                                recPayment.UpdatedDate,
                                recPayment.UpdatedBy,
                                recPayment.Remark as DebtorName ,
                                CASE WHEN recPayment.DebtorSectorID = 0 THEN
                                (SELECT recCorporateAction.CorporateSector
                                FROM transactions.recCorporateAction
                                WHERE recCorporateAction.InvoiceNo = recPayment.InvoiceNo
                                GROUP BY recCorporateAction.CorporateSector
                                )ELSE '' END AS SubSubSectorName,
                                recInvoice.InvoiceValue,
                                '' as SectorName,
                                '' as SubSectorName
                                ,0 UserID
                                FROM
                                transactions.recPayment inner join transactions.recCorporateAction  on recPayment.InvoiceNo = recCorporateAction.InvoiceNo
                                LEFT JOIN master.recDebtorSector ON recPayment.DebtorSectorID=recDebtorSector.DebtorSectorID
                                LEFT JOIN master.recSubSubSector ON recSubSubSector.SubSubSectorID =recDebtorSector.SubSubSectorID
                                LEFT JOIN master.recSector ON recDebtorSector.SectorID=recSector.SectorID
                                LEFT JOIN master.recSubSector ON recDebtorSector.SubSectorID=recSubSector.SubSectorID
                                LEFT JOIN master.recDebtor ON recDebtorSector.DebtorID = recDebtor.DebtorID
                                LEFT JOIN transactions.recInvoice ON recPayment.InvoiceNo = recInvoice.InvoiceNo
                                where recPayment.DebtorSectorID = 0
                                ";
                    #endregion
                }
            }


            if (base.SubSubSectorIDs != null)
            {

                //filter SectorID
                queryPayment = queryPayment + @" WHERE recSector.SectorID IN ( 
                            SELECT  recSector.SectorID
                            FROM master.recSector
                            INNER JOIN master.recSubSector ON recSector.SectorID = recSubSector.SectorID
                            inner join master.recSubSubSector on recSubSubSector.SubSectorID=recSubSector.SubSectorID
                            WHERE recSubSubSector.SubSubSectorID IN(" + base.SubSubSectorIDs + @")
                            GROUP BY recSector.SectorID
                            )";


                queryRegistrasi = queryRegistrasi + @" AND recSector.SectorID IN ( 
                            SELECT  recSector.SectorID
                            FROM master.recSector
                            INNER JOIN master.recSubSector ON recSector.SectorID = recSubSector.SectorID
                            inner join master.recSubSubSector on recSubSubSector.SubSectorID=recSubSector.SubSectorID
                            WHERE recSubSubSector.SubSubSectorID IN(" + base.SubSubSectorIDs + @")
                            GROUP BY recSector.SectorID
                            )";

                
                // filter SubSectorID
                queryPayment = queryPayment + @" AND recSubSector.SubSectorID IN (
                            SELECT DISTINCT recSubSector.SubSectorID
                            FROM master.recSubSector INNER JOIN master.recSubSubSector ON recSubSector.SubSectorID = recSubSubSector.SubSectorID
                            WHERE  recSubSubSector.SubSubSectorID IN (" + base.SubSubSectorIDs + @")
                            GROUP BY recSubSector.SubSectorID
                            )";


                queryRegistrasi = queryRegistrasi + @" AND recSubSector.SubSectorID IN (
                            SELECT DISTINCT recSubSector.SubSectorID
                            FROM master.recSubSector INNER JOIN master.recSubSubSector ON recSubSector.SubSectorID = recSubSubSector.SubSectorID
                            WHERE  recSubSubSector.SubSubSectorID IN (" + base.SubSubSectorIDs + @")
                            GROUP BY recSubSector.SubSectorID
                            )";

               //filter SubSubSectorID
                queryPayment = queryPayment + @" AND recSubSubSector.SubSubSectorID IN (
                            SELECT DISTINCT recSubSubSector.SubSubSectorID
                            FROM master.recSubSubSector 
                            WHERE  recSubSubSector.SubSubSectorID IN (" + base.SubSubSectorIDs + @")
                            GROUP BY recSubSubSector.SubSubSectorID
                            ) ";

                queryRegistrasi = queryRegistrasi + @" AND recSubSubSector.SubSubSectorID IN (
                            SELECT DISTINCT recSubSubSector.SubSubSectorID
                            FROM master.recSubSubSector 
                            WHERE  recSubSubSector.SubSubSectorID IN (" + base.SubSubSectorIDs + @")
                            GROUP BY recSubSubSector.SubSubSectorID
                            ) ";

            }

            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? recPayment.Fields.PaymentID : context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());

            int totalCount = 0;
            int totalPages = 0;
            List<recPayment> pagedList = null;

            try
            {
                totalCount = Convert.ToInt32(recPaymentDal.SelectScalarPembayaran(SQL.Function.Aggregate.Count,recPayment.Fields.PaymentID,queryRegistrasi,queryPayment,queryAksi,parameters));

                decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
                totalPages = Convert.ToInt32(dc);

                pagedList = recPaymentDal.GetListPembayaran(pageSize, page, sidx, sord,queryRegistrasi,queryPayment,queryAksi, parameters);


            }
            catch (Exception ex)
            {
                recLog log = new recLog();
                log.LogEvent = "GetData";
                log.LogMessage = ex.Message;
                log.LogSource = "recPaymenthandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
                log.Dispose();
            }
            finally
            {

            }

                var query = from p in pagedList
                            select new
                            {
                                p.PaymentID,
                                SectorName = p.recDebtorSector.recSector.SectorName,
                                SubSectorName = p.recDebtorSector.recSubSector.SubSectorName,
                                SubSubSectorName = p.recDebtorSector.recSubSubSector.SubSubSectorName,
                                DebtorName = p.recDebtorSector.recDebtor.DebtorName,
                                p.PaymentNo,
                                p.PaymentDate,
                                p.PaymentValue,
                                p.InvoiceNo,
                                InvoiceValue = p.recInvoice.InvoiceValue,
                                selisih = p.recInvoice.InvoiceValue - p.PaymentValue
                            };
            
                // send params into jqGrid's format
                var gridData = new
                {
                    page = page,
                    total = totalPages,
                    records = totalCount,
                    rows = query,//  pagedList,
                };            
           

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);

        }
    }

}